﻿namespace Core
{
    public abstract class AbstractBoard<N> : Entity, IAwake
        where N : AbstractNode, new()
    {
        public DataSource<N> Grid { get; private set; }

        public int W { get; protected set; }
        public int H { get; protected set; }

        public virtual void Awake() { }

        public virtual void Build(UIGrid grid)
        {
            Grid?.Clear();
            Grid = new DataSource<N>(grid.W, grid.H);

            Grid.CreateElements((i, j) =>
            {
                var n = G.Factory.CreateWithParent<N>(this);
                n.Coord = new UnityEngine.Vector2Int(i, j);
                n.Awake(grid.GetChild(i, j), grid.GetPos(i, j));
                return n;
            });
        }
    }
}
